/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.sync.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SyncTraceException;
import com.pitop.sync.dao.SyncTraceDAO;
import com.pitop.sync.entity.SyncTraceState;
import com.pitop.sync.entity.SyncTrace;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("syncTraceSV")
@Slf4j
public class SyncTraceSVImpl extends BaseSVImpl<SyncTrace, Long> implements SyncTraceSV {

    @Autowired
    private SyncTraceDAO syncTraceDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return syncTraceDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(SyncTrace entity) throws BaseException {
        if (entity.getCode() == null) {
            entity.setCode(String.valueOf(uidGenerator.getUID()));
        }
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 保存同步追踪数据
     *
     * @param factoryCode 工厂编码
     * @param syncState   数据库字段:syncState  属性显示:同步状态：成功 SyncSuccess,失败 SyncFailed
     * @param direction   数据库字段:direction  属性显示:同步方向：向下同步 DownwardSync,向上同步 UpwardSync同步方向
     * @param model       数据库字段:model  属性显示:模块(表名)
     * @param dataCode    数据库字段:dataCode  属性显示:数据编码
     * @param summary     数据库字段:summary  属性显示:说明 允许128个汉字
     * @return code       返回保存的数据的Code
     */
    @Override
    public String saveSyncTrace(String factoryCode, String syncState, String direction, String model, String dataCode, String summary) {
        SyncTrace syncTrace = new SyncTrace();
        String code = String.valueOf(uidGenerator.getUID());
        syncTrace.setCode(code);
        syncTrace.setFactoryCode(factoryCode);
        //数据库字段:syncState  属性显示:同步状态：成功 SyncSuccess,失败 SyncFailed
        syncTrace.setSyncState(syncState);
        //数据库字段:direction  属性显示:同步方向：向下同步 DownwardSync,向上同步 UpwardSync同步方向
        syncTrace.setDirection(direction);
        //数据库字段:model  属性显示:模块
        syncTrace.setModel(model);
        //数据库字段:dataCode  属性显示:数据编码
        syncTrace.setDataCode(dataCode);
        //数据库字段:summary  属性显示:说明 允许128个汉字
        syncTrace.setSummary(summary);
        syncTrace.setCreateTime(new Date());
        save(syncTrace);
        return code;
    }

    /**
     * 加载一个对象SyncTrace
     *
     * @param id id* @param code 编码* @param factoryCode 工厂编码
     * @return SyncTrace
     */
    @Override
    public SyncTrace load(java.lang.Long id, java.lang.String code, java.lang.String factoryCode) {
        if (id == null && code == null && factoryCode == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("factoryCode", factoryCode);
        return syncTraceDAO.load(param);
    }

    /**
     * 加载一个对象SyncTrace 通过id
     *
     * @param id id
     * @return SyncTrace
     */
    @Override
    public SyncTrace loadById(java.lang.Long id) {
        if (id == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncTraceDAO.loadById(id);
    }

    /**
     * 加载一个对象SyncTrace 通过code
     *
     * @param code 编码
     * @return SyncTrace
     */
    @Override
    public SyncTrace loadByCode(java.lang.String code) {
        if (code == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncTraceDAO.loadByCode(code);
    }

    /**
     * 加载一个对象SyncTrace 通过factoryCode
     *
     * @param factoryCode 工厂编码
     * @return SyncTrace
     */
    @Override
    public SyncTrace loadByFactoryCode(java.lang.String factoryCode) {
        if (factoryCode == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncTraceDAO.loadByFactoryCode(factoryCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 SyncTrace 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, SyncTraceState newState, SyncTraceState... oldStates) {
        if (id == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        syncTraceDAO.updateStateById(id, newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 SyncTrace 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, SyncTraceState newState, SyncTraceState... oldStates) {
        if (code == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        syncTraceDAO.updateStateByCode(code, newState.name(), states);
    }

    /**
     * 根据主键factoryCode,oldStates 共同更新 SyncTrace 的状态到newState状态
     *
     * @param factoryCode 工厂编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByFactoryCode(java.lang.String factoryCode, SyncTraceState newState, SyncTraceState... oldStates) {
        if (factoryCode == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        syncTraceDAO.updateStateByFactoryCode(factoryCode, newState.name(), states);
    }

    /**
     * 根据主键id 更新 SyncTrace 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, SyncTraceState state) {
        if (id == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncTraceDAO.updateById(id, state.name());
    }

    /**
     * 根据主键code 更新 SyncTrace 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, SyncTraceState state) {
        if (code == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncTraceDAO.updateByCode(code, state.name());
    }

    /**
     * 根据主键factoryCode 更新 SyncTrace 的状态到另一个状态
     *
     * @param factoryCode 工厂编码
     * @param state       状态
     */
    @Override
    public void updateByFactoryCode(java.lang.String factoryCode, SyncTraceState state) {
        if (factoryCode == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncTraceDAO.updateByFactoryCode(factoryCode, state.name());
    }


    /**
     * 删除对象SyncTrace
     *
     * @param id id* @param code 编码* @param factoryCode 工厂编码
     * @return SyncTrace
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code, java.lang.String factoryCode) {
        if (id == null && code == null && factoryCode == null) {
            throw new SyncTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("factoryCode", factoryCode);
        syncTraceDAO.delete(param);
    }


    /**
     * 查询SyncTrace分页
     *
     * @param syncTrace 对象
     * @param offset    查询开始行
     * @param limit     查询行数
     * @return List<SyncTrace>
     */
    @Override
    public List<SyncTrace> list(SyncTrace syncTrace, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (syncTrace != null) {
            map = JSON.parseObject(JSON.toJSONString(syncTrace, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        map.put("sortColumns", " code desc ");
        return syncTraceDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(SyncTrace syncTrace) {
        Map<String, Object> map;
        if (syncTrace != null) {
            map = JSON.parseObject(JSON.toJSONString(syncTrace, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return syncTraceDAO.count(map);
    }

    /**
     * 查询SyncTrace分页
     *
     * @param id     id* @param code  编码* @param factoryCode  工厂编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<SyncTrace>
     */
    @Override
    public List<SyncTrace> list(java.lang.Long id, java.lang.String code, java.lang.String factoryCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("factoryCode", factoryCode);
        return syncTraceDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code, java.lang.String factoryCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("factoryCode", factoryCode);
        return syncTraceDAO.count(param);
    }

}
